added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBSL4DataFormCancelButton / MainPage.xaml.vb
blob3840deb2d1cf26abf7999d1b9b76230ebee507fd
1 '****************************** Module Header ******************************
2 ' Module Name: MainPage.xaml.vb
3 ' Project: VBSL4DataFormCancelButton
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' MainPage's code hehind file.
7 '
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
12 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
13 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
14 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 '***************************************************************************
17 Imports System.Collections.ObjectModel
18 Imports System.ComponentModel
19 Imports System.ComponentModel.DataAnnotations
21 Partial Public Class MainPage
22 Inherits UserControl
24 Public Sub New()
25 InitializeComponent()
26 AddHandler Loaded, AddressOf MainPage_Loaded
28 End Sub
30 Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
31 myDataForm.ItemsSource = New Employee()
32 End Sub
34 End Class
36 ' Your class inherit from IEditableObject to achieve the purpose
37 Public Class EmployeeItem
38 Implements INotifyPropertyChanged
39 Implements IEditableObject
40 Private Structure ItemData
41 Friend EmployeName As String
42 Friend Gender As String
43 Friend Age As Integer
44 Friend OnboardDate As DateTime
45 End Structure
47 Private originalItem As ItemData
48 Private currentItem As ItemData
50 Public Sub New()
51 Me.New("Employee Name", 25, "Male", DateTime.Now)
52 End Sub
54 Public Sub New(ByVal empName As String, ByVal empAge As Integer, ByVal empGender As String, ByVal empOnboardDate As DateTime)
55 EmployeeName = empName
56 Gender = empGender
57 Age = empAge
58 OnboardDate = empOnboardDate
59 End Sub
61 Public Property EmployeeName() As String
62 Get
63 Return currentItem.EmployeName
64 End Get
65 Set(ByVal value As String)
66 If currentItem.EmployeName <> value Then
67 currentItem.EmployeName = value
68 NotifyPropertyChanged("EmployeName")
69 End If
70 End Set
71 End Property
73 Public Property Gender() As String
74 Get
75 Return currentItem.Gender
76 End Get
77 Set(ByVal value As String)
78 If currentItem.Gender <> value Then
79 currentItem.Gender = value
80 NotifyPropertyChanged("Gender")
81 End If
82 End Set
83 End Property
86 <Range(10, 80, ErrorMessage:="Employee's Age range should within 10 to 80")> _
87 <Display(Name:="Age", Description:="Employee's Age")> _
88 Public Property Age() As Integer
89 Get
90 Return currentItem.Age
91 End Get
92 Set(ByVal value As Integer)
93 If currentItem.Age <> value Then
94 Validator.ValidateProperty(value, New ValidationContext(Me, Nothing, Nothing) With { _
95 .MemberName = "Age" _
97 currentItem.Age = value
98 NotifyPropertyChanged("Age")
99 End If
100 End Set
101 End Property
103 Public Property OnboardDate() As DateTime
105 Return currentItem.OnboardDate
106 End Get
107 Set(ByVal value As DateTime)
108 If value <> currentItem.OnboardDate Then
109 currentItem.OnboardDate = value
110 NotifyPropertyChanged("OnboardDate")
111 End If
112 End Set
113 End Property
116 #Region "INotifyPropertyChanged Members"
118 Public Event PropertyChanged As PropertyChangedEventHandler _
119 Implements INotifyPropertyChanged.PropertyChanged
121 Private Sub NotifyPropertyChanged(ByVal info As String)
122 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
123 End Sub
125 #End Region
127 #Region "IEditableObject Members"
129 ' Copy the original value before editing
130 Public Sub BeginEdit() Implements IEditableObject.BeginEdit
132 originalItem = currentItem
133 End Sub
135 ' Restore the original value if edit operation is cancelled.
136 Public Sub CancelEdit() Implements IEditableObject.CancelEdit
137 currentItem = originalItem
138 NotifyPropertyChanged("")
140 End Sub
142 Public Sub EndEdit() Implements IEditableObject.EndEdit
144 End Sub
146 #End Region
148 End Class
150 Public Class Employee
151 Inherits ObservableCollection(Of EmployeeItem)
152 Public Sub New()
153 Add((New EmployeeItem("Steven", 20, "Male", New DateTime(2010, 9, 1))))
154 Add((New EmployeeItem("Vivian", 26, "Female", New DateTime(2008, 5, 1))))
155 Add((New EmployeeItem("Bill", 28, "Male", New DateTime(2006, 2, 2))))
156 Add((New EmployeeItem("Janney", 30, "Female", New DateTime(2004, 3, 10))))
157 Add((New EmployeeItem("Bob", 40, "Male", New DateTime(2009, 5, 27))))
158 Add((New EmployeeItem("Jonathan", 27, "Male", New DateTime(2000, 10, 25))))
159 End Sub
161 End Class